-- UIAirShipSkillExplain
-- Create by cheny3
-- 秘宝帮助界面

--UIAirShipSkillExplain继承自Layer
UIAirShipSkillExplain = class("UIAirShipSkillExplain", function()
    return cc.Layer:create();
end);

function UIAirShipSkillExplain.create(title, desc, btnText, btnCallback, extraDesc)
    return UIAirShipSkillExplain.new(title, desc, btnText, btnCallback, extraDesc);
end

--构造函数
function UIAirShipSkillExplain:ctor(title, desc, btnText, btnCallback, extraDesc)
    self:setName("UIAirShipSkillExplain");

    -- 初始化/复用炼金说明界面
    local node = cc.CSLoader:createNode("layout/sky_city/SkyCityTeamDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 指定了标题和内容/否则->秘宝帮助界面
    self.title = title or getLocStr("airship_skill_explain_title");
    self.desc = desc or getLocStr("airship_skill_explain");
    self.btnText = btnText;
    self.btnCallback = btnCallback;

    if type(extraDesc) == "table" then
        self.title2 = extraDesc["title"];
        self.desc2 = extraDesc["desc"];
        self.showContent2 = true;
    end

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 重绘
function UIAirShipSkillExplain:redraw()
    local bg = findChildByName(self.node, "CT/bg");
    -- 标题
    local titleLabel = findChildByName(self.node,"CT/title");
    TextStyleM.setTextStyle(titleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    titleLabel:setString(self.title);

    -- 说明
    local descText = self.desc;
    local descLabel = findChildByName(self.node, "CT/desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, descText);


    local titleLabel2 = findChildByName(self.node,"CT/title2");
    local descLabel2 = findChildByName(self.node, "CT/desc2");
    titleLabel2:setVisible(false);
    descLabel2:setVisible(false);

    if self.showContent2 then
        titleLabel2:setVisible(true);
        descLabel2:setVisible(true);
        TextStyleM.setTextStyle(titleLabel2,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
        titleLabel2:setString(self.title2);

        -- 说明
        local descText2 = self.desc2;
        TextStyleM.setTextStyle(descLabel2, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        descLabel2 = applyString(descLabel2, descText2);
    end

    local btnOk = findChildByName(self.node, "CT/btn_ok");
    if self.btnText and self.btnCallback then
        btnOk:setVisible(true);
        TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btnOk:setTitleText(self.btnText);

        local function onOkClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                -- 点击确定按钮
                AudioM.playFx("button_return");
                self.btnCallback();
            end
        end
        btnOk:addTouchEventListener(onOkClick);

        -- 自适应所有组件
        AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,{ titleLabel, descLabel, btnOk,}, {DEFAULT_GAP, DEFAULT_GAP, DEFAULT_GAP}, 40, 40);
    elseif self.showContent2 then
        btnOk:setVisible(false);
        -- 自适应所有组件
        AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,{ titleLabel, descLabel, titleLabel2, descLabel2}, {DEFAULT_GAP, DEFAULT_GAP}, 40, 40);
    else
        btnOk:setVisible(false);
        -- 自适应所有组件
        AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,{ titleLabel, descLabel, }, {DEFAULT_GAP, DEFAULT_GAP}, 40, 40);
    end
end

-- 注册点击事件
function UIAirShipSkillExplain:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("ship_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipSkillExplain");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册点击任意地方关闭当前窗口事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIAirShipSkillExplain");
        end
    end
    btnBgBack:addTouchEventListener(onBgBackClick);
end

-- 适配
function UIAirShipSkillExplain:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


